<template>
  <div>
    <el-input placeholder="输入关键字进行过滤" v-model="filterText"></el-input>
    <el-tree
      :data="menus"
      :props="defaultProps"
      node-key="catId"
      ref="menuTree"
      @node-click="nodeclick"
      :filter-node-method="filterNode"
      :highlight-current = "true"
    />
  </div>
</template>

<script>
export default {
  data () {
    return {
      filterText: '',
      menus: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      expandedKey: [],
      dialogVisible: false,
      dialogFormVisible: false,
      formLabelWidth: '120px'
    }
  },
  // 监控data中的数据变化
  watch: {
    filterText (val) {
      this.$refs.menuTree.filter(val)
    }
  },
  methods: {
    getMenus () {
      this.$http({
        url: this.$http.adornUrl('/product/category/list/tree'),
        method: 'get'
      }).then(({data}) => {
        // console.log('成功获取数据', data.data)
        this.menus = data.data
      })
    },
    nodeclick (data, node, component) {
      this.$emit('tree-node-click', data, node, component)
    },
    // 树节点过滤
    filterNode (value, data) {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    }
  },
  created () {
    this.getMenus()
  }
}
</script>

<style scoped>
  .el-input--medium {
    width: 306px;
  }
</style>